package controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import model.Projectdeclaration;
import service.ProjectdelarationService;
import service.impl.ProjectdelarationServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author CY
 * @date 2022/12/17 10:01
 **/
@WebServlet("/pdi")
public class ProjectdeclarationInsertServlet extends HttpServlet {
    ProjectdelarationService service = new ProjectdelarationServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求,响应格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;utf-8");
        //Json解析
        final BufferedReader bufferedReader = new BufferedReader(req.getReader());
        String line = null;
        final StringBuilder json = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            json.append(line);
        }
        final JSONObject jsonObject = JSON.parseObject(json.toString());
        final Projectdeclaration projectdeclaration = new Projectdeclaration();
        projectdeclaration.setDesc(jsonObject.getString("desc"));
        projectdeclaration.setPassbyt(jsonObject.getLong("passbyt"));
        projectdeclaration.setTid(jsonObject.getLong("tid"));
        projectdeclaration.setTdesc(jsonObject.getString("tdesc"));
        projectdeclaration.setPassbyc(jsonObject.getLong("passbyc"));
        //构造结果
        JSONObject message = new JSONObject();
        if (service.projectedlarationAdd(projectdeclaration) == 1) {
            message.put("status", 1);
            message.put("Msg", "添加成功!");
        } else {
            message.put("status", 0);
            message.put("Msg", "添加失败!");
        }
        //返回结果
        resp.getWriter().write(message.toString());

    }
}
